package com.example.yichuangyun.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemOrderBinding;
import com.example.yichuangyun.databinding.LayoutEmptyList2Binding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.business.OrderModel;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * viewType--分别为item以及空view
     */
    private final int VIEW_TYPE_ITEM = 1;
    private static final int VIEW_TYPE_EMPTY = 0;
    private final Context context;
    private List<OrderModel> mData;

    public OrderAdapter(Context context, List<OrderModel> mData) {
        this.context = context;
        this.mData = mData;
    }

    public void onRefresh(List<OrderModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void addData(List<OrderModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        if (viewType == VIEW_TYPE_EMPTY) {
            LayoutEmptyList2Binding emptyListBinding = LayoutEmptyList2Binding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemOrderBinding itemOrderBinding = ItemOrderBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(itemOrderBinding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            OrderModel model = mData.get(position);
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.binding.tvName.setText(CommonUtil.checkText(model.getProductName()));
            myViewHolder.binding.tvOrderNum.setText(String.format(context.getString(R.string.order_no),
                    !TextUtils.isEmpty(model.getOrderNo()) ? model.getOrderNo() : context.getString(R.string.no)));
            // 订单状态 0 、待激活 1、服务中 2、服务完成 3、已关闭
            switch (model.getOrderStatus()) {
                case 0:
                    myViewHolder.binding.tvServiceState.setText(context.getString(R.string.to_be_activated));
                    break;
                case 1:
                    myViewHolder.binding.tvServiceState.setText(context.getString(R.string.serving));
                    break;
                case 2:
                    myViewHolder.binding.tvServiceState.setText(context.getString(R.string.service_finish));
                    break;
                case 3:
                    myViewHolder.binding.tvServiceState.setText(context.getString(R.string.closed));
                    break;
            }
            myViewHolder.binding.tvSignPerson.setText(String.format(context.getString(R.string.sign_person), model.getXiaosrName()));
            myViewHolder.binding.tvSignDate.setText(String.format(context.getString(R.string.sign_date),
                    !TextUtils.isEmpty(model.getSignDate()) ? DateUtil.getYearMonthDay(model.getSignDate()) : context.getString(R.string.no)));
        } else if (holder instanceof EmptyViewHolder) {
            EmptyViewHolder emptyHolder = (EmptyViewHolder) holder;
            emptyHolder.binding.text.setText(context.getString(R.string.no_order));
        }
    }

    @Override
    public int getItemCount() {
        //同时这里也需要添加判断，如果mData.size()为0的话，只引入一个布局，就是emptyView
        // 那么，这个recyclerView的itemCount为1
        if (mData.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在这里进行判断，如果我们的集合的长度为0时，我们就使用emptyView的布局
        if (mData.size() == 0) {
            return VIEW_TYPE_EMPTY;
        }
        //如果有数据，则使用ITEM的布局
        return VIEW_TYPE_ITEM;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemOrderBinding binding;

        public MyViewHolder(ItemOrderBinding itemOrderBinding) {
            super(itemOrderBinding.getRoot());
            binding = itemOrderBinding;
        }
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {

        LayoutEmptyList2Binding binding;

        public EmptyViewHolder(LayoutEmptyList2Binding emptyListBinding) {
            super(emptyListBinding.getRoot());
            binding = emptyListBinding;
        }
    }
}
